<?php
class SP_Common_Application
{
	private $app_dir;
	private $name;
	private $modules_dir='modules';
	private $parts_dir='parts';
	private $templates_dir='templates';
	private $configs_dir='configs';
	
	public function __construct($name)
	{
		$this->name=$name;
		$this->app_dir=SP_Config::get('system')->apps_dir.'/'.$name;
		SP_Path::set('app', $this->app_dir);
		if( !is_dir(SP_Config::getIncludeDir().$this->getAppDir()) ) 
			throw new Exception("Directory for application '$name' not exist");
		
	}
	
	public function getAppDir()
	{
		return $this->app_dir.'/';
	}

	public function getModulesDir()
	{
		return $this->getAppDir().$this->modules_dir.'/';
	}
	
	public function setModulesDir($value)
	{
		$this->modules_dir=$value;
	}
	
	public function getPartsDir()
	{
		return $this->getAppDir().$this->parts_dir.'/';
	}
	
	public function setPartsDir($value)
	{
		$this->parts_dir=$value;
	}
	
	public function getTemplatesDir()
	{
		return $this->getAppDir().$this->templates_dir.'/';
	}
	
	public function setTemplatesDir($value)
	{
		$this->templates_dir=$value;
	}
	
	public function getConfigsDir()
	{
		return $this->getAppDir().$this->configs_dir.'/';
	}
	
	public function setConfigsDir($value)
	{
		$this->configs_dir=$value;
	}
	
	public function start()
	{
		if( !SP_Config_Configurator::readConfig('app', $this->getConfigsDir()."app.php") )
			throw new Exception('Can\'t read app.php');
		if( SP_Config::get('app')->has('model_dir') )
			SP_Loader::addModelDir(SP_Config::get('app')->model_dir, $this->getAppDir()); 
		$page = new SP_Common_Page($this);
		$page->render();
	}
	
	
}
?>